﻿namespace WP7Contrib.Services.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Device.Location;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;
    using BingMaps;
    using BingMaps.Model;
    using Caching;
    using Logging;
    using Microsoft.Phone.Controls.Maps;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    [TestClass]
    public class BingMapsTests : SilverlightTest
    {
        [TestMethod]
        public void ShouldBeAbleToCacheLocationCriterion()
        {
            // Log...
            var log = new DebugLog();

            // Given a location criterion...
            var criterion = CriterionFactory.CreateLocationSearchForAddress("40 Beatty Road", "United Kingdom", "N16");

            // Given we have a isolated storage cache provider...
            var cacheProvider = new IsolatedStorageCacheProvider("BingMapsTests", Enumerable.Empty<Assembly>(), log);
            
            // When we test for cacheability...
            IEnumerable<Type> failingTypes = null;
            var cacheable = cacheProvider.IsCacheable(criterion, ref failingTypes);

            //Then we expect it to be cacheable...
            Assert.IsTrue(cacheable);
            Assert.IsTrue(failingTypes.Count() == 0);
        }

        [TestMethod]
        public void ShouldBeAbleToCacheRouteCriterion()
        {
            // Log...
            var log = new DebugLog();

            // Given two points for a route...
            var waypoint1 = new WayPoint {Address = {PostalCode = "N16 8EB"}};
            var waypoint2 = new WayPoint {Address = {PostalCode = "LE17 5ER"}};

            // Given a route criterion...
            var criterion = CriterionFactory.CreateRouteSearch(new List<WayPoint>{ waypoint1, waypoint2 });

            // Given we have a isolated storage cache provider...
            var cacheProvider = new IsolatedStorageCacheProvider("BingMapsTests", Enumerable.Empty<Assembly>(), log);

            // When we test for cacheability...
            IEnumerable<Type> failingTypes = null;
            var cacheable = cacheProvider.IsCacheable(criterion, ref failingTypes);

            //Then we expect it to be cacheable...
            Assert.IsTrue(cacheable);
            Assert.IsTrue(failingTypes.Count() == 0);
        }

        [TestMethod]
        public void ShouldBeAbleToCacheSearchCriterion()
        {
            // Log...
            var log = new DebugLog();

            // Given a search criterion...
            var criterion = CriterionFactory.CreateSearchForServices(new LocationRect(new GeoCoordinate(51.554111, -0.072784), 20, 20), "taxi", CultureInfo.CurrentCulture);

            // Given we have a isolated storage cache provider...
            var cacheProvider = new IsolatedStorageCacheProvider("BingMapsTests", Enumerable.Empty<Assembly>(), log);

            // When we test for cacheability...
            IEnumerable<Type> failingTypes = null;
            var cacheable = cacheProvider.IsCacheable(criterion, ref failingTypes);

            //Then we expect it to be cacheable...
            Assert.IsTrue(cacheable);
            Assert.IsTrue(failingTypes.Count() == 0);
        }   
    }
}

